package com.sunlg.smarthome.ui.personal;

import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.ActivityManager;
import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.base.BaseActivity;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.contract.LogoutContract;
import com.sunlg.smarthome.contract.UpdateUserContract;
import com.sunlg.smarthome.contract.presenter.LogoutPresenter;
import com.sunlg.smarthome.contract.presenter.UpdateUserPresenter;
import com.sunlg.smarthome.data.entity.User;
import com.sunlg.smarthome.data.remote.MySubscriber;
import com.sunlg.smarthome.data.remote.Result;
import com.sunlg.smarthome.ui.main.LoginActivity;
import com.sunlg.smarthome.ui.main.MainActivity;
import com.sunlg.smarthome.utils.PreferHelper;

import butterknife.BindView;

public class PersonalActivity extends BaseActivity
        implements UpdateUserContract.View, View.OnClickListener{

    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_email)
    TextView tvEmail;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.btn_logout)
    Button btnLogout;

    @BindView(R.id.rl_email)
    RelativeLayout rlEmail;
    @BindView(R.id.rl_phone)
    RelativeLayout rlPhone;
    @BindView(R.id.rl_update_pwd)
    RelativeLayout rlUpdatePwd;

    private User user;
    private PreferHelper preferHelper;
    private UpdateUserContract.Presenter presenter;

    private UpdateMsgDialog updateMsgDialog;
    private UpdatePwdDialog updatePwdDialog;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_personal;
    }

    @Override
    protected void setupToolBar(Toolbar toolbar) {
        super.setupToolBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("个人信息");
        actionBar.setDisplayHomeAsUpEnabled(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doFinish();
            }
        });
    }

    @Override
    protected void init() {
        user = new User();
        presenter = new UpdateUserPresenter();
        presenter.attachView(this);
        preferHelper = App.getInstance().getPreferHelper();

        setUser();

        updateMsgDialog = new UpdateMsgDialog(this);
        updatePwdDialog = new UpdatePwdDialog(this);
    }

    @Override
    protected void initView() {
        if(user != null){
            tvName.setText(user.getUsername());
            tvEmail.setText(user.getEmail());
            tvPhone.setText(user.getPhone());
        }

        btnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                App.getInstance()
                        .getServiceWrapper()
                        .logout(new MySubscriber<Result>() {
                            @Override
                            public void onSuccess(Result data) {
                                forward(LoginActivity.class);
                                new LogoutPresenter().logout();
                                ActivityManager.getInstance().finishActivity(MainActivity.class);
                            }

                            @Override
                            public void _onError(Throwable e) {
                                toast(e.getMessage());
                            }
                        });
            }
        });
    }

    @Override
    protected void registerEvents() {
        super.registerEvents();
        rlEmail.setOnClickListener(this);
        rlPhone.setOnClickListener(this);
        rlUpdatePwd.setOnClickListener(this);
    }

    @Override
    public void updateSuccess(User u) {
        user = u;
        tvName.setText(u.getUsername());
        tvEmail.setText(u.getEmail());
        tvPhone.setText(u.getPhone());
        toast("更新成功");
    }

    @Override
    public void showError(String msg) {

    }

    private void setUser(){
        user.setId(preferHelper.getInt(C.PreKey.USER_ID, 0));
        user.setEmail(preferHelper.getString(C.PreKey.EMAIL, ""));
        user.setUsername(preferHelper.getString(C.PreKey.USER_NAME, ""));
        user.setPhone(preferHelper.getString(C.PreKey.PHONE, ""));
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if(id == R.id.rl_email){
            showUpdateEmailDialog();
        }else if(id == R.id.rl_phone){
            showUpdatePhoneDialog();
        }else if(id == R.id.rl_update_pwd){
            showUpdatePwdDialog();
        }
    }

    private void showUpdateEmailDialog(){
        updateMsgDialog.setOnEditTextInputListener(new UpdateMsgDialog.OnEditTextInputListener() {
            @Override
            public void onEditTextInput(String input) {
                User u = new User();
                u.setId(user.getId());
                if(!TextUtils.isEmpty(input)){
                    u.setEmail(input);
                }else {
                    toast("输入不能为空");
                }

                presenter.updateUser(u);
            }
        });
        updateMsgDialog.show("修改邮箱", "邮箱");
    }

    private void showUpdatePhoneDialog(){

        updateMsgDialog.setOnEditTextInputListener(new UpdateMsgDialog.OnEditTextInputListener() {
            @Override
            public void onEditTextInput(String input) {
                User u = new User();
                u.setId(user.getId());
                if(!TextUtils.isEmpty(input)){
                    u.setPhone(input);
                }else {
                    toast("输入不能为空");
                }

                presenter.updateUser(u);
            }
        });
        updateMsgDialog.show("修改手机号", "手机号");
    }

    private void showUpdatePwdDialog(){
        updatePwdDialog.setOnEditTextInputListener(new UpdatePwdDialog.OnEditTextInputListener() {
            @Override
            public void onEditTextInput(String inputText1, String inputText2) {
                if(!TextUtils.isEmpty(inputText1) && !TextUtils.isEmpty(inputText2)){
                    if(inputText1.equals(inputText2)){
                        User u = new User();
                        u.setId(user.getId());
                        u.setPassword(inputText2);
                        presenter.updateUser(u);
                    }else {
                        toast("密码不匹配");
                    }
                }else {
                    toast("密码或确认密码不能为空");
                }
            }
        });
        updatePwdDialog.show();
    }
}
